********************************************************************************
* All states
********************************************************************************

use dataset_voters, clear

*** year dummies ***

tabulate year, gen(year_)

*** for long diff ***

gen post = 1 if year == 2012
replace post = 0 if year == 2000
gen anyfrackingXpost = anyfracking*post

*** make ideology within-state ***

rename di_state state
egen sy_ideology_st = mean(sy_ideology), by(state)
gen sy_ideology_dm = sy_ideologymodel - sy_ideology_st
xtile qideo = sy_ideologymodel, nq(5)
xtile qideost = sy_ideology_dm, nq(5)
tabulate qideost, gen(qideost_)

*** interactions between ideo and frack ***

capture program drop myrecoder
program define myrecoder
	args D
	capture drop d*
	gen d = `D'
	forvalues i = 1(1)5 {
		gen d_`i' = d * qideost_`i'
	}
end

*** do the regressions ***

eststo clear
myrecoder lnwells
eststo: reg turnout qideost_2 qideost_3 qideost_4 qideost_5 ///
	d d_2 d_3 d_4 d_5 year_2 year_3 year_4 year_5, a(zipcode) cl(zipcode)
estadd local ZFE = "Yes"
estadd local YFE = "Yes"
myrecoder anywells
eststo: reg turnout qideost_2 qideost_3 qideost_4 qideost_5 ///
	d d_2 d_3 d_4 d_5 year_2 year_3 year_4 year_5, a(zipcode) cl(zipcode)
estadd local ZFE = "Yes"
estadd local YFE = "Yes"
myrecoder anyfrackingXpost
eststo: reg turnout qideost_2 qideost_3 qideost_4 qideost_5 ///
	d d_2 d_3 d_4 d_5 year_4, a(zipcode) cl(zipcode)
estadd local ZFE = "Yes"
estadd local YFE = "Yes"

********************************************************************************
* High fracking states
********************************************************************************

keep if highfracking == 1

*** re-make ideology within-state ***

drop sy_ideology_st sy_ideology_dm qideo qideost*
egen sy_ideology_st = mean(sy_ideology), by(state)
gen sy_ideology_dm = sy_ideologymodel - sy_ideology_st
xtile qideo = sy_ideologymodel, nq(5)
xtile qideost = sy_ideology_dm, nq(5)
tabulate qideost, gen(qideost_)

*** re-do the regressions ***

myrecoder lnwells
eststo: reg turnout qideost_2 qideost_3 qideost_4 qideost_5 ///
	d d_2 d_3 d_4 d_5 year_2 year_3 year_4 year_5, a(zipcode) cl(zipcode)
estadd local ZFE = "Yes"
estadd local YFE = "Yes"
myrecoder anywells
eststo: reg turnout qideost_2 qideost_3 qideost_4 qideost_5 ///
	d d_2 d_3 d_4 d_5 year_2 year_3 year_4 year_5, a(zipcode) cl(zipcode)
estadd local ZFE = "Yes"
estadd local YFE = "Yes"
myrecoder anyfrackingXpost
eststo: reg turnout qideost_2 qideost_3 qideost_4 qideost_5 ///
	d d_2 d_3 d_4 d_5 year_4, a(zipcode) cl(zipcode)
estadd local ZFE = "Yes"
estadd local YFE = "Yes"

********************************************************************************
* Output
********************************************************************************

label variable year_2 "2004"
label variable year_3 "2008"
label variable year_4 "2012"
label variable year_5 "2016"

label variable d "Frack"
label variable d_2 "Frack X q2"
label variable d_3 "Frack X q3"
label variable d_4 "Frack X q4"
label variable d_5 "Frack X q5"

label variable qideost_2 "q2"
label variable qideost_3 "q3"
label variable qideost_4 "q4"
label variable qideost_5 "q5"

#delimit;

esttab 
	using "_output/tableA10.tex"
	,
	replace
		booktabs
		order(d d_2 d_3 d_4 d_5 
			qideost_2 qideost_3 qideost_4 qideost_5)
		cells(b(fmt(3) star) se(par))
		drop(_cons year_*)
		compress
		label
		width(\linewidth)
		gap
		collabels(none)
		mtitles("Log wells" "Any wells" "Ever X post" "Log wells" 
			"Any wells" "Ever X post")
		mgroups(
			"All states" "High-fracking states", 
			pattern(1 0 0 1 0 0)
			prefix(\multicolumn{@span}{c}{) suffix(})
			span erepeat(\cmidrule(lr){@span})
		)
		stats(
			YFE
			ZFE
			N_clust
			N
			,
				label(
				"Year fixed effects"
				"Zip code fixed effects"
				"Clusters"
				"Observations"
				)
				fmt(0 0 %20.0fc)
		)	
		;
		
#delimit cr

********************************************************************************
* End
********************************************************************************


